<?LassoScript
// Last modified 11/30/09 by Eric Landmann

// FUNCTIONALITY
// Include file used in response page to add a forum
// last modified: 4/24/08 by JRM, Landmann InterActive

// CHANGE NOTES
// 11/30/09
// Worked on admin area

// Debugging
// Var:'svDebug' = 'Y';

// Converting action_params
Var:'vError' = '';
Var:'vOption' = '';
// The action_param "Action" is passed by the frm_forums.inc form to indicate whether it is an add or edit
// Action = Update, means the request was an Edit (from frm_forums.inc)
// Action = Add, means the request was an Add (from frm_forums.inc)
Var:'vAction' = (Action_Param:'Action');

If: $svDebug == 'Y';
	'<br>\r';
	'<b>topic_addresponse.inc</b><br>\r';
/If;

// Checking Action to make sure it's only posting from the Add page
// ============= ADD ========
If: $vAction == 'Add';

	// Convert action_params
	Include:($svLibsPath)'topic_params.inc';

	// If an error was generated from the Params, no point continuing, so bail immediately
	If: $vError == '';

		// Create name to display
		Var:'vDisplayName' = $vTopic_Title;

		// Query Forum database
		// Searching for Topic Title
		// If it does not exist, then add it. This avoids multiple entries of the same title
		Var:'SQLSearchTopics'='SELECT Topic_Title from ' $svTopicsTable ' WHERE Topic_Title = "' $vTopic_Title '"';
		Inline: $IV_SearchTopics, -SQL=$SQLSearchTopics;
	
			If: $svDebug == 'Y';
				'27: SQLSearchTopics = ' $SQLSearchTopics '<br>\r';
				'27: Found_Count =  ' (Found_Count) '<br>\r';
			/If;
	
// If no forum found, add the record
			If: (Found_Count) == 0;
	
				// Construct the query to add the Forum
				Var:'vSQLAddTopic' = '
				INSERT INTO ' $svTopicsTable '  (
					Topic_ID,
					Section_ID,
					Topic_Title,
					Poster,
					Allow
					) 
				VALUES ("",
					"'(Encode_SQL:(Var:'vSID'))'",
					"'(Encode_SQL:(Var:'vTopic_Title'))'",
					"'(Encode_SQL:(Var:'vPoster'))'",
					"'(Encode_SQL:(Var:'vAllow'))'"
					);';
			
						If: $svDebug == 'Y';
							'128: vSQLAddTopic = ' $vSQLAddTopic '<br>\n';
						/If;
			
				Inline: $IV_AddTopic, -SQL=$vSQLAddTopic;
					
					If:(Error_CurrentError)==(Error_NoError);
				
						// Get new record ID value from LAST_INSERT_ID() for link
						Inline: -SQL='SELECT LAST_INSERT_ID() AS NewTopicID';
							Var:'vNewTopicID' = (Field:'NewTopicID');
						/Inline;


						// If no error, Add was successful, dump out Error 1004 "Add Successful"
						Var:'vError' = '1004';
						Var:'vOption' = 'Topic';

					// There was an error
					Else;
						Var:'vError' = (Error_CurrentError);
					/If;
				
				/Inline;
			
			// Forum found in Sections table, output 2006 "Duplicate Forum" error
			Else;
				Var:'vError' = '2006';
			/If;	

		/Inline;

	/If;

	If: $svDebug == 'Y';
		'137: vError = ' $vError '<br>\n';
		'137: vOption = ' $vOption '<br>\n';
	/If;
// ============= EDIT ========
// Action = Update, means the request came from the Edit page
Else: $vAction == 'Update';

	// Convert action_params
	Include:($svLibsPath)'topic_params.inc';

	// Copying  record ID to vNewTopicID to build link
	// This is so we can just use vNewTopicID at bottom of page for both add and edit
	Var:'vNewTopicID'=(Var:'vTID');

	// If an error was generated from the Params, no point continuing, so bail immediately
	If: $vError == '';

		// Construct the query to Update the Topic
		// Not changing the Date_Created fields
		Var:'vSQLUpdateTopics' = '
		UPDATE ' $svTopicsTable ' SET
			Topic_ID = "'(Encode_SQL:(Var:'vTID'))'",
			Section_ID = "'(Encode_SQL:(Var:'vSID'))'",
			Topic_Title = "'(Encode_SQL:(Var:'vTopic_Title'))'",
			Poster = "'(Encode_SQL:(Var:'vPoster'))'",
			Allow = "'(Encode_SQL:(Var:'vAllow'))'"
			WHERE Topic_ID = "' $vTID '"';

		If: $svDebug == 'Y';
			'224: vSQLUpdateTopics = ' $vSQLUpdateTopics '<br>\n';
		/If;
	
		Inline: $IV_UpdateTopics, -SQL=$vSQLUpdateTopics;
			
			// If no error, Update was successful, dump out Error 5022 "Update Successful"
			If:(Error_CurrentError)==(Error_NoError);

				Var:'vError' = '5022';
				Var:'vOption' = 'Topic';
				If: $svDebug == 'Y';
					'233: vError = ' $vError '<br>\n';
					'233: vOption = ' $vOption '<br>\n';
				/If;

			// There was an error
			Else;
				Var:'vError' = (Error_CurrentError);
			/If;

			// If topic has been archived, now update all the posts to be archived
			If: $vAllow == 'A';
				Var:'vSQLUpdatePosts' = 'UPDATE ' $svPostsTable ' SET Allow = "A" WHERE Topic_ID = "' $vTID '"';
				Inline: $IV_SearchPosts, -SQL=$vSQLUpdatePosts;
					// Probably should add error checking!
				/Inline;

				// Updating Stats Table
				// Get total of topic from Stats
				Var:'vSQL_Query_Stats'='SELECT The_Count FROM ' $svSiteDatabase '.' $svStatsTable ' WHERE Section_ID="' (Var:'vSID') '" LIMIT 1;';
				
				Inline: $IV_SearchStats, -SQL=$vSQL_Query_Stats;
				
					// Copy field to var, subtract number of topics archived, update the Count
					Records;
						Var:'Old_Count'=(Field:'The_Count');
						Var:'New_Count'=(Math_Sub: (Var:'Old_Count'),1);
					/Records;
				/Inline;
				// Put new total back to Stats
				Var:'vSQL_Query_StatsUpdate'='UPDATE ' $svStatsTable ' SET The_Count=' (Var:'New_Count') ', Last_Post_Date="' (Server_Date:-Extended) '" WHERE Section_ID = "' (Var:'vSID') '";';
				
				Inline: $IV_UpdateStats, -SQL=$vSQL_Query_StatsUpdate; /Inline;


			/If;

		/Inline;

	// vError == ''
	/If;
		
// ============= DELETE ========
Else: $vAction == 'Delete';

	// Convert action_params
	Var:'vTID'=(Action_Param:'TID');
	Var:'vSID'=(Action_Param:'SID');
		
	// Build the Delete Query
	Var:'SQLDeleteQuery' = (string);
	Var:'SQLDeleteQuery' = 'DELETE FROM ' $svSiteDatabase'.'$svTopicsTable ' WHERE Topic_ID = "' $vTID '"';
	
	// Delete the record
	Inline: $IV_SearchTopics, -SQL=$SQLDeleteQuery;
		// If delete successful, output 1021 "Deletion Successful" and go ahead and delete attachments,
		If: (Error_Code) == '0';
			Var:'vError' = '1021';
			Var:'vOption' = $vTID;

		Else;
			Var:'vError' = '5025';
			Var:'vOption' = (Error_CurrentError);
			Var:'URL' = ('setup_search.lasso?DataType=Topic&Error='$vError'&Option='$vOption);
			If: $svDebug == 'Y';
				'175: URL: ' $URL '<br>\n';
				'<a href="'$URL'">' $URL '</a><br>\n';
			Else;
				Redirect_URL: $URL;
			/If;
		/If;
	/Inline;

	// Updating Stats Table
	// Get total of topic from Stats
	Var:'vSQL_Query_Stats'='SELECT The_Count FROM ' $svSiteDatabase '.' $svStatsTable ' WHERE Section_ID="' (Var:'vSID') '" LIMIT 1;';
	
	Inline: $IV_SearchStats, -SQL=$vSQL_Query_Stats;
	
		// Copy field to var, subtract number of topics archived, update the Count
		Records;
			Var:'Old_Count'=(Field:'The_Count');
			Var:'New_Count'=(Math_Sub: (Var:'Old_Count'),1);
		/Records;
	/Inline;
	// Put new total back to Stats
	Var:'vSQL_Query_StatsUpdate'='UPDATE ' $svStatsTable ' SET The_Count=' (Var:'New_Count') ', Last_Post_Date="' (Server_Date:-Extended) '" WHERE Section_ID = "' (Var:'vSID') '";';
	
	Inline: $IV_UpdateStats, -SQL=$vSQL_Query_StatsUpdate; /Inline;



// Referrer not good, somebody is messing with us
// Dump out error 9002, "Database Error"
		
// Referrer not good, somebody is messing with us
// Dump out error 9002, "Database Error"
Else;
	Var:'vError' = '9002';
/If;


// Deal with the results of the processing

// ============= ADD ========
// If any other error other than 1004 "Add Successful" do a redirect
If: $vAction == 'Add';
	// Standard Error Table
	If: (Var:'vError') == '1004';
		li_ShowError3: -ErrNum=(Var:'vError'), -Option=(Var:'vOption');

// Edit New Listing Link
?>
<div align="left">
	View the record for <a href="setup_editrecord.lasso?DataType=Topic&TID=[Var:'vNewTopicID']&New=Y"><b>[$vTopic_Title]</b></a>
</div>
<?Lassoscript
	Else;
		Var:'URL' = ('setup_add.lasso?Error='$vError'&Option='$vOption'&'(Client_POSTArgs));
		If: $svDebug == 'Y';
			'273: URL = ' $URL '<br>';
			'<a href="'$URL'">' $URL '</a><br>\n';
		Else;
			Redirect_URL: $URL;
		/If;
	/If;
/If;

// ============= EDIT ========
// If vAction = Edit, Always redirect to edit page
// If record updated OK, will get a 5022 "Update Successful" message, but do NOT pass the Params, only the ID
// This forces the system to do a new lookup
If: $vAction == 'Update';
	If: (Var:'vError') == '5022';
		Var:'URL' = ('setup_editrecord.lasso?DataType=Topic&TID='$vNewTopicID'&New=Y&Error='$vError'&Option='$vOption);
	Else;
		Var:'URL' = ('setup_editrecord.lasso?New=Y&Error='$vError'&Option='$vOption'&'(Client_POSTArgs));
	/If;
	If: $svDebug == 'Y';
		'294: URL = ' $URL '<br>';
		'<a href="'$URL'">' $URL '</a><br>\n';
	Else;
		Redirect_URL: $URL;
	/If;
/If;

// ============= DELETE ========
// If Action = Delete, redirect to Customer Info Search page
If: $vAction == 'Delete';
	Var:'URL' = ('setup_search.lasso?Datatype=Topic&Error='$vError'&Option='$vOption);
	If: $svDebug == 'Y';
		'239: URL = ' $URL '<br>';
		'<a href="'$URL'">' $URL '</a><br>\n';
	Else;
		Redirect_URL: $URL;
	/If;
/If;
?>
